package com.kifas.service.dataaccess;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kifas.entity.GroupEntity;

@Repository
public class GroupDao {

	@Autowired
	private SessionFactory sessionFactory;

	private static final String ALL_GROUP = "FROM GroupEntity g";
	private static final String GROUP_BY_ID = ALL_GROUP + " where g.idGroup = :idGroup";
	
	public GroupEntity getGroupById(Long id) {
		return (GroupEntity) sessionFactory.getCurrentSession().createQuery(GROUP_BY_ID).setParameter("idGroup", id).uniqueResult();
	}

	public void insert(GroupEntity group) throws Exception {
		sessionFactory.getCurrentSession().save(group);
	}

	@SuppressWarnings("unchecked")
	public List<GroupEntity> getGroups() {
		return sessionFactory.getCurrentSession().createQuery(ALL_GROUP).list();
	}
	
}
